<?php
/**
 * SocialEngine
 *
 * @category   Application_Core
 * @package    User
 * @copyright  Copyright 2006-2010 Webligo Developments
 * @license    http://www.socialengine.net/license/
 * @version    $Id: AdminManageController.php 9414 2011-10-19 22:10:29Z john $
 * @author     John
 */

/**
 * @category   Application_Core
 * @package    User
 * @copyright  Copyright 2006-2010 Webligo Developments
 * @license    http://www.socialengine.net/license/
 */
class User_AdminManageController extends Core_Controller_Action_Admin {
	public function indexAction() {
		$this->view->formFilter = $formFilter = new User_Form_Admin_Manage_Filter ();
		$page = $this->_getParam ( 'page', 1 );
		
		$table = Engine_Api::_ ()->getDbtable ( 'users', 'user' );
		$select = $table->select ();
		
		// Process form
		$values = array ();
		if ($formFilter->isValid ( $this->_getAllParams () )) {
			$values = $formFilter->getValues ();
		}
		
		foreach ( $values as $key => $value ) {
			if (null === $value) {
				unset ( $values [$key] );
			}
		}
		
		$values = array_merge ( array ('order' => 'user_id', 'order_direction' => 'DESC' ), $values );
		
		$this->view->assign ( $values );
		
		// Set up select info
		$select->order ( (! empty ( $values ['order'] ) ? $values ['order'] : 'user_id') . ' ' . (! empty ( $values ['order_direction'] ) ? $values ['order_direction'] : 'DESC') );
		
		if (! empty ( $values ['displayname'] )) {
			$select->where ( 'displayname LIKE ?', '%' . $values ['displayname'] . '%' );
		}
		
		if (! empty ( $values ['username'] )) {
			$select->where ( 'username LIKE ?', '%' . $values ['username'] . '%' );
		}
		
		if (! empty ( $values ['email'] )) {
			$select->where ( 'email LIKE ?', '%' . $values ['email'] . '%' );
		}
		
		if (! empty ( $values ['level_id'] )) {
			$select->where ( 'level_id = ?', $values ['level_id'] );
		}
		
		if (isset ( $values ['enabled'] ) && $values ['enabled'] != - 1) {
			$select->where ( 'enabled = ?', $values ['enabled'] );
		}
		
		// Filter out junk
		$valuesCopy = array_filter ( $values );
		// Reset enabled bit
		if (isset ( $values ['enabled'] ) && $values ['enabled'] == 0) {
			$valuesCopy ['enabled'] = 0;
		}
		
		// Make paginator
		$this->view->paginator = $paginator = Zend_Paginator::factory ( $select );
		$this->view->paginator = $paginator->setCurrentPageNumber ( $page );
		$this->view->formValues = $valuesCopy;
		
		$this->view->superAdminCount = count ( Engine_Api::_ ()->user ()->getSuperAdmins () );
		$this->view->hideEmails = _ENGINE_ADMIN_NEUTER;
	
		//$this->view->formDelete = new User_Form_Admin_Manage_Delete();
	}
	
	public function multiModifyAction() {
		if ($this->getRequest ()->isPost ()) {
			$values = $this->getRequest ()->getPost ();
			foreach ( $values as $key => $value ) {
				if ($key == 'modify_' . $value) {
					$user = Engine_Api::_ ()->getItem ( 'user', ( int ) $value );
					if ($values ['submit_button'] == 'delete') {
						if ($user->level_id != 1) {
							$user->delete ();
						}
					} else if ($values ['submit_button'] == 'approve') {
						$old_status = $user->enabled;
						$user->enabled = 1;
						$user->approved = 1;
						$user->save ();
						
						// Send a notification that the account was not approved previously
						if ($old_status == 0) {
							Engine_Api::_ ()->getApi ( 'mail', 'core' )->sendSystem ( $user, 'user_account_approved', array ('host' => $_SERVER ['HTTP_HOST'], 'email' => $user->email, 'date' => time (), 'recipient_title' => $user->getTitle (), 'recipient_link' => $user->getHref (), 'recipient_photo' => $user->getPhotoUrl ( 'thumb.icon' ), 'object_link' => 'http://' . $_SERVER ['HTTP_HOST'] . Zend_Controller_Front::getInstance ()->getRouter ()->assemble ( array (), 'user_login', true ) ) );
						}
					}
				}
			}
		}
		
		return $this->_helper->redirector->gotoRoute ( array ('action' => 'index' ) );
	}
	
	public function editAction() {
		$id = $this->_getParam ( 'id', null );
		$this->view->user = $user = Engine_Api::_ ()->getItem ( 'user', $id );
		$this->view->form = $form = new User_Form_Admin_Manage_Edit ( array ('userIdentity' => $id ) );
		
		// Do not allow editing level if the last superadmin
		if ($user->level_id == 1 && count ( Engine_Api::_ ()->user ()->getSuperAdmins () ) == 1) {
			$form->removeElement ( 'level_id' );
		}
		
		// Get values
		$values = $user->toArray ();
		unset ( $values ['password'] );
		if (_ENGINE_ADMIN_NEUTER) {
			unset ( $values ['email'] );
		}
		
		// Get networks
		$select = Engine_Api::_ ()->getDbtable ( 'membership', 'network' )->getMembershipsOfSelect ( $user );
		$networks = Engine_Api::_ ()->getDbtable ( 'networks', 'network' )->fetchAll ( $select );
		$values ['network_id'] = $oldNetworks = array ();
		foreach ( $networks as $network ) {
			$values ['network_id'] [] = $oldNetworks [] = $network->getIdentity ();
		}
		
		// Populate form
		$form->populate ( $values );
		
		// Check method/valid
		if (! $this->getRequest ()->isPost ()) {
			return;
		}
		if (! $form->isValid ( $this->getRequest ()->getPost () )) {
			return;
		}
		
		$values = $form->getValues ();
		
		// Check password validity
		if (empty ( $values ['password'] ) && empty ( $values ['password_conf'] )) {
			unset ( $values ['password'] );
			unset ( $values ['password_conf'] );
		} else if ($values ['password'] != $values ['password_conf']) {
			return $form->getElement ( 'password' )->addError ( 'Passwords do not match.' );
		} else {
			unset ( $values ['password_conf'] );
		}
		
		// Process
		$oldValues = $user->toArray ();
		
		// Set new network
		$userNetworks = $values ['network_id'];
		unset ( $values ['network_id'] );
		if ($userNetworks == NULL) {
			$userNetworks = array ();
		}
		$joinIds = array_diff ( $userNetworks, $oldNetworks );
		foreach ( $joinIds as $id ) {
			$network = Engine_Api::_ ()->getItem ( 'network', $id );
			$network->membership ()->addMember ( $user )->setUserApproved ( $user )->setResourceApproved ( $user );
		}
		$leaveIds = array_diff ( $oldNetworks, $userNetworks );
		foreach ( $leaveIds as $id ) {
			$network = Engine_Api::_ ()->getItem ( 'network', $id );
			if (! is_null ( $network )) {
				$network->membership ()->removeMember ( $user );
			}
		}
		
		$user->setFromArray ( $values );
		$user->save ();
		
		// Send a notification that the account has been approved
		if (! $oldValues ['enabled'] && $values ['enabled']) {
			Engine_Api::_ ()->getApi ( 'mail', 'core' )->sendSystem ( $user, 'user_account_approved', array ('host' => $_SERVER ['HTTP_HOST'], 'email' => $user->email, 'date' => time (), 'recipient_title' => $user->getTitle (), 'recipient_link' => $user->getHref (), 'recipient_photo' => $user->getPhotoUrl ( 'thumb.icon' ), 'object_link' => 'http://' . $_SERVER ['HTTP_HOST'] . Zend_Controller_Front::getInstance ()->getRouter ()->assemble ( array (), 'user_login', true ) ) );
			// Send hook to add activity
			Engine_Hooks_Dispatcher::getInstance ()->callEvent ( 'onUserEnable', $user );
		} else if ($oldValues ['enabled'] && ! $values ['enabled']) {
			// @todo ?
		}
		
		// Forward
		return $this->_forward ( 'success', 'utility', 'core', array ('smoothboxClose' => true, 'parentRefresh' => true, 'format' => 'smoothbox', 'messages' => array ('Your changes have been saved.' ) ) );
	}
	
	public function deleteAction() {
		$id = $this->_getParam ( 'id', null );
		$this->view->user = $user = Engine_Api::_ ()->getItem ( 'user', $id );
		$this->view->form = $form = new User_Form_Admin_Manage_Delete ();
		// deleting user
		//$form->user_id->setValue($id);
		

		if ($this->getRequest ()->isPost ()) {
			$db = Engine_Api::_ ()->getDbtable ( 'users', 'user' )->getAdapter ();
			$db->beginTransaction ();
			
			try {
				$user->delete ();
				
				$db->commit ();
			} catch ( Exception $e ) {
				$db->rollBack ();
				throw $e;
			}
			
			return $this->_forward ( 'success', 'utility', 'core', array ('smoothboxClose' => true, 'parentRefresh' => true, 'format' => 'smoothbox', 'messages' => array ('This member has been successfully deleted.' ) ) );
		}
	}
	
	public function loginAction() {
		$id = $this->_getParam ( 'id' );
		$user = Engine_Api::_ ()->getItem ( 'user', $id );
		
		// @todo change this to look up actual superadmin level
		if ($user->level_id == 1 || ! $this->getRequest ()->isPost ()) {
			if (null === $this->_helper->contextSwitch->getCurrentContext ()) {
				return $this->_helper->redirector->gotoRoute ( array ('action' => 'index', 'id' => null ) );
			} else {
				$this->view->status = false;
				$this->view->error = true;
				return;
			}
		}
		
		// Login
		Zend_Auth::getInstance ()->getStorage ()->write ( $user->getIdentity () );
		
		// Redirect
		if (null === $this->_helper->contextSwitch->getCurrentContext ()) {
			return $this->_helper->redirector->gotoRoute ( array (), 'default', true );
		} else {
			$this->view->status = true;
			return;
		}
	}
	
	public function statsAction() {
		$id = $this->_getParam ( 'id', null );
		$this->view->user = $user = Engine_Api::_ ()->getItem ( 'user', $id );
		
		$fieldsByAlias = Engine_Api::_ ()->fields ()->getFieldsObjectsByAlias ( $user );
		
		if (! empty ( $fieldsByAlias ['profile_type'] )) {
			$optionId = $fieldsByAlias ['profile_type']->getValue ( $user );
			if ($optionId) {
				$optionObj = Engine_Api::_ ()->fields ()->getFieldsOptions ( $user )->getRowMatching ( 'option_id', $optionId->value );
				if ($optionObj) {
					$this->view->memberType = $optionObj->label;
				}
			}
		}
		
		// Networks
		$select = Engine_Api::_ ()->getDbtable ( 'membership', 'network' )->getMembershipsOfSelect ( $user )->where ( 'hide = ?', 0 );
		$this->view->networks = Engine_Api::_ ()->getDbtable ( 'networks', 'network' )->fetchAll ( $select );
		
		// Friend count
		$this->view->friendCount = $user->membership ()->getMemberCount ( $user );
	}
}
